<template>
  <div>
    <!-- 面包屑组件 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>角色管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card>
      <el-col :span="4">
        <el-button type="primary" class="v">添加角色</el-button>
      </el-col>
      <!-- 表格 -->
      <el-table :data="roleList" border stripe>
        <el-table-column type="expand">
          <template v-slot="scoped">
            <!-- {{ scoped.row }} -->
            <el-row
              v-for="(item, ind) in scoped.row.children"
              :key="item.id"
              :class="['bdbotton', ind == 0 ? 'bdtop' : '', 'vcenten']"
            >
              <!-- 一级权限 -->
              <el-col :span="6">
                <el-tag closable @close="removeTag(scoped.row, item.id)">
                  {{ item.authName }}
                </el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <!-- 二级和三级权限 -->
              <el-col :span="18">
                <el-row
                  v-for="(item1, ind1) in item.children"
                  :key="item1.id"
                  :class="['vcenten', ind1 === 0 ? '' : 'bdtop']"
                >
                  <el-col :span="6">
                    <el-tag
                      type="success"
                      closable
                      @close="removeTag(scoped.row, item1.id)"
                    >
                      {{ item1.authName }}
                    </el-tag>
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <el-col :span="18">
                    <el-tag
                      type="warning"
                      v-for="item2 in item1.children"
                      :key="item2.id"
                      closable
                      @close="removeTag(scoped.row, item2.id)"
                    >
                      {{ item1.authName }}
                    </el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column type="index" label="#"> </el-table-column>
        <el-table-column label="角色名称" prop="roleName"></el-table-column>
        <el-table-column label="角色描述" prop="roleDesc"></el-table-column>
        <el-table-column label="操作" width="320px">
          <template v-slot="scoped">
            <el-button type="primary" icon="el-icon-edit" size="mini"
              >编辑</el-button
            >
            <el-button type="danger" icon="el-icon-delete" size="mini"
              >删除</el-button
            >
            <el-button
              type="warning"
              icon="el-icon-setting"
              size="mini"
              @click="showDialong(scoped.row)"
              >分配权限</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 分配权限 -->
    <el-dialog
      title="分配权限"
      :visible.sync="ShowdialogVisible"
      width="50%"
      @close="closeFn"
    >
      <!-- 树形控件 -->
      <el-tree
        :data="rightList"
        :props="rightTree"
        show-checkbox
        node-key="id"
        :default-expand-all="true"
        :default-checked-keys="defaultKey"
        ref="TreeRef"
      ></el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="ShowdialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="allotRightsFn">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 添加角色 -->
    <!-- 删除角色 -->
    <!-- 编辑角色 -->
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 保存角色列表
      roleList: [],
      ShowdialogVisible: false,
      rightList: [],
      // 所有权限列表
      rightTree: {
        label: "authName",
        children: "children",
      },
      // 勾选的id
      defaultKey: [],
      // 保存角色Id
      roleID: "",
    };
  },
  created() {
    this.getRoles();
  },
  methods: {
    async getRoles() {
      const { data: res } = await this.$axios.get("roles");
      if (res.meta.status !== 200)
        return this.$message.error("获取角色列表失败");
      this.roleList = res.data;
      // console.log(this.roleList);
    },
    async removeTag(rolesInfo, id) {
      const res = await this.$confirm(
        "此操作将永久删除该标签, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      );
      if (res !== "confirm") return this.$message.info("您取消的删除操作");
      const { data: res1 } = await this.$axios.delete(
        `roles/${rolesInfo.id}/rights/${id}`
      );
      if (res1.meta.status !== 200) return this.$message.error("删除权限失败");
      rolesInfo.children = res1.data;
      // this.$message.success("删除成功");
    },
    // 展示分配角色功能
    async showDialong(role) {
      this.roleID = role.id;
      const { data: res } = await this.$axios.get("rights/tree");
      if (res.meta.status !== 200) return this.$message.error("获取权限失败");
      this.rightList = res.data;
      // console.log(this.rightList);
      this.getliftKey(role, this.defaultKey);
      this.ShowdialogVisible = true;
    },
    // 节点勾选
    getliftKey(node, arr) {
      if (!node.children) return arr.push(node.id);
      node.children.forEach((item) => {
        this.getliftKey(item, arr);
      });
    },
    // 关闭对话框清除保存节点ID
    closeFn() {
      this.defaultKey = [];
    },
    // 点击为角色分配角色
    async allotRightsFn() {
      const Key = [
        ...this.$refs.TreeRef.getHalfCheckedKeys(),
        ...this.$refs.TreeRef.getCheckedKeys(),
      ];
      // console.log(Key);
      const keyStr = Key.join(",");
      // console.log(keyStr);
      // console.log(this.roleID);
      const { data: res } = await this.$axios.post(
        `roles/${this.roleID}/rights`,
        { rids: keyStr }
      );
      // console.log(res);
      if (res.meta.status !== 200)
        return this.$message.error("分配角色管理失败");
      this.$message.success("分配角色管理成功");
      this.getRoles()
      this.ShowdialogVisible = false;
    },
  },
};
</script>

<style lang="less" scoped>
.el-tag {
  margin: 20px;
}
// .el-col {
//   margin-bottom: 20px;
// }
.bdbotton {
  border-bottom: 1px solid #ccc;
}
.bdtop {
  border-top: 1px solid #ccc;
}
.vcenten {
  display: flex;
  align-items: center;
}
.v {
  margin-bottom: 20px;
}
</style>